#version 440


layout(location = 0) in vec2 qt_TexCoord;

layout(location = 0) out vec4 fragColor;

layout(std140, binding = 0) uniform buf {
    mat4 qt_Matrix;
    float qt_Opacity;
} ubuf;

layout(binding = 1) uniform sampler2D qt_TextureY;
layout(binding = 2) uniform sampler2D qt_TextureU;
layout(binding = 3) uniform sampler2D qt_TextureV;

void main()
{
    vec3 yuv;
    vec3 rgb;
    yuv.x = texture(qt_TextureY, qt_TexCoord).r;
    yuv.y = texture(qt_TextureU, qt_TexCoord).r - 0.5;
    yuv.z = texture(qt_TextureV, qt_TexCoord).r - 0.5;

    // ref https://mymusing.co/bt-709-yuv-to-rgb-conversion-color
    
    rgb.r = clamp( yuv.x + 1.5748 *yuv.z, 0, 1);
    rgb.g = clamp( yuv.x - 0.187324 * yuv.y - 0.468124 * yuv.z, 0, 1);
    rgb.b = clamp( yuv.x + 1.8556 * yuv.y, 0, 1);
    fragColor = vec4(rgb, 1);
}
